//
//  MyDownloadTableViewCell.m
//  DolphinMusic
//
//  Created by dlios on 15-4-4.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "MyDownloadTableViewCell.h"

@implementation MyDownloadTableViewCell
- (void)dealloc
{
    [_song release];
    [_titleLabel release];
    [_authorLabel release];
    [_deleteButton release];
    [_array release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.titleLabel];
        [_titleLabel release];
        
        self.authorLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.authorLabel];
        [_authorLabel release];
        
        self.deleteButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.contentView addSubview:self.deleteButton];
        self.array = [NSMutableArray array];
        
        
        
        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat width = self.contentView.frame.size.width;
    CGFloat height = self.contentView.frame.size.height;
    
    self.titleLabel.frame = CGRectMake(10, 0, width - 20, height * 2 / 3);
    self.titleLabel.text = self.song.title;
    self.titleLabel.font = [UIFont systemFontOfSize:14];
   
    self.authorLabel.frame = CGRectMake(10, height * 2 / 3, width - 20, height / 3);
    self.authorLabel.text = self.song.author;
    self.authorLabel.font = [UIFont systemFontOfSize:12];
    self.authorLabel.textColor = [UIColor lightGrayColor];
    self.deleteButton.frame = CGRectMake(width - 60, height / 2 - 10, 60, 20);
    self.deleteButton.layer.cornerRadius = 5;
    self.deleteButton.layer.borderColor = [[UIColor lightGrayColor]CGColor];
    self.deleteButton.layer.borderWidth = 1;
    [self.deleteButton setTitle:@"删除" forState:UIControlStateNormal];
    self.deleteButton.tintColor = [UIColor grayColor];
    [self.deleteButton addTarget:self action:@selector(deleteButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
}
- (void)deleteButtonAction:(UIButton *)button
{
    // 删除已下载的歌曲
    NSLog(@"delete");
    DataBaseHandler *dbHandeler = [DataBaseHandler shareInstance];
 
    [dbHandeler deleteTableFilePath:self.song.song_id];
    
    // 通知中心
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center postNotificationName:@"MyDownloadControllerReload" object:nil];
    
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    
    NSString *filePath = [NSString stringWithFormat:@"%@/%@.mp3", cachePath, self.song.song_id];

  //  NSLog(@"filePath = %@", filePath);
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager removeItemAtPath:filePath error:nil];
    
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
